package com.recommender;

import java.sql.ResultSet;
import java.util.*;

import Neighborhood.Neighborhood;

import com.model.DataModel;
import com.model.StructuredContextDataModel;
import com.similarity.ContextSimilarity;
import com.similarity.UserSimilarity;

import edu.emory.mathcs.backport.java.util.TreeMap;

public class UserBasedRecommender extends StructuredContextRecommender{
	
	UserSimilarity uSim;
	Neighborhood neighborhood;
	

	@Override
	public Map<Integer,Double> getRecommendations(
			HashMap<String, String> contextMap, String keyword, long currentUserID,
			DataModel datamodel,Neighborhood neighborhood) {
		
		genericmodel=(StructuredContextDataModel)datamodel;
		ResultSet tuples=null;
		uSim=new UserSimilarity(genericmodel);
		cSim=new ContextSimilarity(genericmodel);
		this.neighborhood=neighborhood;
		String[] contextVariables=genericmodel.getContextVariables();
		

		boolean inserted=false;
		HashMap<Integer, Double> myHashMap = new HashMap<Integer,Double>();
		Map<Integer, Double> imageResultsMap=new TreeMap();
		
		
		
			
		
				
		try {
			tuples=genericmodel.getContextDetails(keyword);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
						
			while(tuples.next())
			{
				double predictedRank=0.0f;
				int imageID=tuples.getInt("ImageID");
				int userid=tuples.getInt("UserID");
				for (String s : contextVariables) {
					
					if(s.equals("Keyword"))continue;
					else if(s.equals("UserID"))continue;
					else	
					{
						int count=0;
						String contextVariable=s;
						String contextValue= tuples.getString(contextVariable);
						String currentContextValue= contextMap.get(contextVariable.toLowerCase());
						double contextSimilarity=cSim.calculateSimilarity(contextVariable,contextValue,currentContextValue);
						if(contextSimilarity>=0.9)
						{
							count++;
						}
						
						double rankForContext=getRankings(imageID, contextVariable, currentContextValue);
						System.out.println("freq "+rankForContext);
						predictedRank += contextSimilarity*rankForContext;
						
						if(count==3)
						{
							predictedRank++;
						}
						
						System.out.println("cSim "+contextSimilarity);
						
				
					}
				}
				
				double userSimilarity=uSim.userSimilarity(currentUserID, userid);
				System.out.println("uSim "+userSimilarity);
				predictedRank*=userSimilarity;
				
				System.out.println("rank "+predictedRank);
				System.out.println();
				Double rank=0.0d;
				
				
				
				if(myHashMap.containsKey(imageID))
				{
					rank=(Double)myHashMap.get(imageID);
					inserted=true;
				}
				else inserted=false;
		
				
				if(!inserted)
				{
					myHashMap.put(imageID, predictedRank);
				}
				else
				{
					Double newrank=predictedRank + rank;
					myHashMap.put(imageID,newrank);
				}
				
				
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		imageResultsMap=neighborhood.getFilteredImages(myHashMap);
		return imageResultsMap;
	}
	

	
	

}
